package com.aspose.words.maven;

import java.io.File;
import java.net.URI;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import com.aspose.words.Activator;
import com.aspose.words.maven.utils.MavenSettings;

public class AsposeMavenProjectWizardPage extends WizardPage {

	public static final String PROP_PROJECT_NAME = "projectName";
	public static final String PROP_GROUP_ID = "groupId";

	private Text txtProjectLocation;
	private Text txtProjectName;
	private Text txtProjectFolder;
	private Text txtArtifactId;
	private Text txtGroupId;
	private Text txtVersion;
	private Text txtPackage;
	private Button chkDownloadExamples;

	/**
	 * Create the wizard.
	 */
	public AsposeMavenProjectWizardPage() {
		super("wizardPage");
		setTitle("New Project");
		setDescription("Name and Location");
	}

	private String getDefaultProjectName() {
		String defaultName = "asposemavenproject";
		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		try {
			for (int i = 1; i < 100; i++) {

				boolean match = false;
				for (IProject project : projects) {
					if (project.getDescription().getName().equals(defaultName + i)) {
						match = true;
						break;
					}

				}
				if (!match) {
					defaultName = defaultName + i;
					break;
				}
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
		return defaultName;
	}

	private void initControls() {
		txtGroupId.setText(MavenSettings.getDefault().getLastArchetypeGroupId());
		txtVersion.setText(MavenSettings.getDefault().getLastArchetypeVersion());
		txtProjectLocation.setText(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString());
		txtProjectName.setText(getDefaultProjectName());
		txtProjectFolder.setText(txtProjectLocation.getText() + File.separator + txtProjectName.getText());
		txtArtifactId.setText(txtProjectName.getText());
		txtPackage.setText("com.mycompany." + txtProjectName.getText());
	}

	public String getProjectName() {
		return txtProjectName.getText();
	}

	public String getPackageName() {
		return txtPackage.getText();
	}

	public URI getLocationURI() {
		return new File(txtProjectFolder.getText()).toURI();
	}

	public boolean isDownloadExamplesChecked() {
		return chkDownloadExamples.getSelection();
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);

		Label lblNewLabel = new Label(container, SWT.NONE);
		lblNewLabel.setImage(SWTResourceManager.getImage(Activator.getResourceFilePath("long_banner.png")));
		lblNewLabel.setBounds(10, 0, 564, 95);

		Label lblPleaseEnterProject = new Label(container, SWT.NONE);
		lblPleaseEnterProject.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblPleaseEnterProject.setBounds(5, 98, 179, 15);
		lblPleaseEnterProject.setText("Please enter project detail:");

		Label lblPleaseEnterMaven = new Label(container, SWT.NONE);
		lblPleaseEnterMaven.setText("Please enter maven artifact detail:");
		lblPleaseEnterMaven.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblPleaseEnterMaven.setBounds(5, 194, 213, 15);

		Label lblProjectName = new Label(container, SWT.NONE);
		lblProjectName.setBounds(5, 118, 84, 15);
		lblProjectName.setText("Project Name:");

		Label lblProjectLocation = new Label(container, SWT.NONE);
		lblProjectLocation.setBounds(5, 146, 94, 15);
		lblProjectLocation.setText("Project Location:");

		Label lblProjectFolder = new Label(container, SWT.NONE);
		lblProjectFolder.setText("Project Folder:");
		lblProjectFolder.setBounds(5, 173, 94, 15);

		Label lblArtifactId = new Label(container, SWT.NONE);
		lblArtifactId.setText("Artifact Id:");
		lblArtifactId.setBounds(5, 215, 94, 15);

		Label lblGroupId = new Label(container, SWT.NONE);
		lblGroupId.setText("Group Id:");
		lblGroupId.setBounds(5, 242, 94, 15);

		Label lblVersion = new Label(container, SWT.NONE);
		lblVersion.setText("Version:");
		lblVersion.setBounds(5, 270, 94, 15);

		Label lblPackage = new Label(container, SWT.NONE);
		lblPackage.setText("Package:");
		lblPackage.setBounds(5, 296, 94, 15);

		txtProjectLocation = new Text(container, SWT.BORDER);
		txtProjectLocation.setBounds(118, 143, 370, 21);

		Button btnNewButton = new Button(container, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(new Shell(), SWT.NULL);
				String path = dialog.open();
				if (path != null) {
					txtProjectLocation.setText(path);
				}
			}
		});
		btnNewButton.setBounds(494, 140, 75, 25);
		btnNewButton.setText("Browse...");

		txtProjectName = new Text(container, SWT.BORDER);
		txtProjectName.setBounds(118, 116, 370, 21);

		txtProjectFolder = new Text(container, SWT.BORDER);
		txtProjectFolder.setEditable(false);
		txtProjectFolder.setBounds(118, 170, 370, 21);

		txtArtifactId = new Text(container, SWT.BORDER);
		txtArtifactId.setEnabled(false);
		txtArtifactId.setEditable(false);
		txtArtifactId.setBounds(118, 212, 370, 21);

		txtGroupId = new Text(container, SWT.BORDER);
		txtGroupId.setBounds(118, 239, 370, 21);

		txtVersion = new Text(container, SWT.BORDER);
		txtVersion.setBounds(118, 266, 370, 21);

		txtPackage = new Text(container, SWT.BORDER);
		txtPackage.setBounds(118, 293, 370, 21);

		chkDownloadExamples = new Button(container, SWT.CHECK);
		chkDownloadExamples.setBounds(5, 325, 492, 16);
		chkDownloadExamples.setText("Also Download Code Example (for using Aspose.Words for Java)");

		Label lblNewLabel_1 = new Label(container, SWT.NONE);
		lblNewLabel_1.setBounds(494, 296, 55, 15);
		lblNewLabel_1.setText("(Optional)");

		initControls();
	}
}
